package com.pineapple.module.web.standards.fertilizer;

import java.sql.Connection;
import java.util.Calendar;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.pineapple.module.core.standards.fertilizer.domain.Fertilizer;
import com.pineapple.module.core.standards.fertilizer.domain.FertilizerCriteria;
import com.pineapple.module.core.standards.fertilizer.domain.FertilizerModel;
import com.pineapple.module.core.standards.fertilizer.service.FertilizerManager;

import com.pineapple.module.util.connection.DBCPConnection;

public class FertilizerAction extends ActionSupport implements ModelDriven<FertilizerModel> {

	private static final long serialVersionUID = -3661000161146714152L;
	FertilizerModel model = new FertilizerModel();

	public String init(){

		return "search";
	}


	public String search(){
		String criteriaKey = "";
		FertilizerCriteria criteria = model.getCriteria();
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		try(Connection conn = new DBCPConnection().getConnection()){
			FertilizerManager manager = new FertilizerManager();

			if (model.getCriteriaKey() != null && !model.getCriteriaKey().equals("")) {
				criteria = (FertilizerCriteria) session.getAttribute(model.getCriteriaKey());
				/*int startrecord = model.getStart();
				criteria.setStart(startrecord);*/
				criteriaKey = model.getCriteriaKey();
				model.setCriteria(criteria);
			} else {
				Calendar calendar = Calendar.getInstance();
				criteriaKey = String.valueOf(calendar.getTimeInMillis());
				criteria.setCriteriaKey(criteriaKey);
			}
			model.setLstFertilizerSearch(manager.search(model.getCriteria(), conn));
			if (model.getLstFertilizerSearch().size() == 0) {
				ServletActionContext.getRequest().setAttribute("message",getText("0002"));
			}
			model.setCriteria(criteria);
			model.setCriteriaKey(criteriaKey);
			session.setAttribute(criteriaKey, criteria);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return"search";
	}

	public String  gotoAdd(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		model.setPage("add");
		return "addEdit";
	}

	public String add(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		try(Connection conn = new DBCPConnection().getConnection()){
			FertilizerManager manager = new FertilizerManager();
			manager.add(conn, model.getFertilizer());
			model.setFertilizer(new Fertilizer());
			ServletActionContext.getRequest().setAttribute("message",getText("0004"));
		}catch(Exception ex){
			ServletActionContext.getRequest().setAttribute("message",getText("00011"));
			ex.printStackTrace();
		}
		return "addEdit";
	}
	public String edit(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		try(Connection conn = new DBCPConnection().getConnection()){
			FertilizerManager manager = new FertilizerManager();
			manager.edit(conn, model.getFertilizer());
			model.setFertilizer(new Fertilizer());
			ServletActionContext.getRequest().setAttribute("message",getText("0006"));
		}catch(Exception ex){
			ServletActionContext.getRequest().setAttribute("message",getText("0005"));
			ex.printStackTrace();
		}
		return "searchDo";
	}

	public String searchById(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		try(Connection conn = new DBCPConnection().getConnection()){
			int id = 0;
			if(ServletActionContext.getRequest().getParameter("fertilizerId") != null){
				id = Integer.parseInt(ServletActionContext.getRequest().getParameter("fertilizerId"));
			}
			if(ServletActionContext.getRequest().getParameter("page") != null){
				String page = ServletActionContext.getRequest().getParameter("page");
				model.setPage(page);
				FertilizerManager manager = new FertilizerManager();
				model.setFertilizer(manager.searchById(conn,id));
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "addEdit";
	}

	public String delete(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		String ids = "";
		if(ServletActionContext.getRequest().getParameter("fertilizerIds") != null){
			ids = ServletActionContext.getRequest().getParameter("fertilizerIds");
		}
		try(Connection conn = new DBCPConnection().getConnection()){
				FertilizerManager manager = new FertilizerManager();
				manager.delete(conn,ids);
				ServletActionContext.getRequest().setAttribute("message",getText("0009"));
		}catch(Exception ex){
			ex.printStackTrace();
			ServletActionContext.getRequest().setAttribute("message",getText("0010"));
		}
		return "searchDo";
	}

	public String clear(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		model.setCriteria(new FertilizerCriteria());
		return "search";
	}
	@Override
	public FertilizerModel getModel() {
		// TODO Auto-generated method stub
		return model;
	}








}
